var TOOL = 0x01;
var LIVE = 0x02;
var CONTAINER = 0x04;

var DEFAULT_TICKS = 5;
var DEFAULT_STATUS = "READY";

var inputTags = new Array();
	inputTags[0] = "SELECT";
	inputTags[1] = "INPUT";
	inputTags[2] = "BUTTON";

function TimeData()
{
	this.dataFetch = 0;
	this.dataProc = 0;
	this.mapFetch = 0;
	this.mapProc = 0;
	this.customMapFetch = 0;
	this.customMapProc = 0;
	this.toString = timeDataString;
	return this;
}

function timeData2Str(tm)
{
	var s = "";
	
	s += "Time taken: data(";
	s += tm.dataFetch + "+";
	s += tm.dataProc + ")+map(";
	s += tm.mapFetch + "+";
	s += tm.mapProc + ")+custom(";
	s += tm.customMapFetch + "+";
	s += tm.customMapProc + ")+=";
	s += (tm.dataFetch + tm.dataProc + tm.mapFetch + tm.mapProc + tm.customMapFetch + tm.customMapProc);
	
	return s;	
}

function timeDataString()
{
	var s = "";
	
	s += "Time taken: data(";
	s += this.dataFetch + "+";
	s += this.dataProc + ")+map(";
	s += this.mapFetch + "+";
	s += this.mapProc + ")+custom(";
	s += this.customMapFetch + "+";
	s += this.customMapProc + ")+=";
	s += (this.dataFetch + this.dataProc + this.mapFetch + this.mapProc + this.customMapFetch + this.customMapProc);
	
	return s;	
}

function resetTimeData(tm)
{
	tm.dataFetch = 0;
	tm.dataProc = 0;
	tm.mapFetch = 0;
	tm.mapProc = 0;
	tm.customMapFetch = 0;
	tm.customMapProc = 0;
}

var PROT 	= 0x01;
var NUM 	= PROT << 1;
var BRT		= PROT << 2;
var DET		= PROT << 3;
var FSET	= PROT << 4;
var DRK		= PROT << 5;
var ASKIP	= PROT << 6;
var UNPROT	= PROT << 7;

/* ALL attributes that have FSET or MDT (modified data tag) */
  /*  R    UNPROT NUM BRT DET FSET  */
  /*  )    UNPROT NUM DRK FSET      */
  /*  N    UNPROT NUM DET FSET      */
  /*  I    UNPROT BRT DET FSET      */
  /*  J    UNPROT NUM FSET          */
  /*  (    UNPROT DRK FSET          */
  /*  E    UNPROT DET FSET          */
  /*  A    UNPROT FSET              */
  /*  9    PROT ASKIP BRT DET FSET  */
  /*  '    PROT ASKIP DRK FSET      */
  /*  5    PROT ASKIP DET FSET      */
  /*  Z    PROT BRT DET FSET        */
  /*  1    PROT ASKIP FSET          */
  /*  _    PROT DRK FSET            */
  /*  V    PROT DET FSET            */
  /*  /    PROT FSET                */

/* All the other attributes */
  /*  Q    UNPROT NUM BRT DET       */
  /*  *    UNPROT NUM DRK           */
  /*  M    UNPROT NUM DET           */
  /*  H    UNPROT BRT DET           */
  /*  D    UNPROT DET               */
  /*  <    UNPROT DRK               */
  /*  &    UNPROT NUM               */
  /*       UNPROT                   */
  /*  8    PROT ASKIP BRT DET       */
  /*  4    PROT ASKIP DET           */
  /*  @    PROT ASKIP DRK           */
  /*  Y    PROT BRT DET             */
  /*  %    PROT DRK                 */
  /*  U    PROT DET                 */
  /*  0    PROT ASKIP               */
  /*  -    PROT                     */
  
var ATTR_MAP = 
{	
	'!': 0,
	'R': UNPROT | NUM | BRT | DET | FSET,
	')': UNPROT | NUM | DRK | FSET,
	'N': UNPROT | NUM | DET | FSET,
	'I': UNPROT | BRT | DET | FSET,
	'J': UNPROT | NUM | FSET,
	'(': UNPROT | DRK | FSET,
	'E': UNPROT | DET | FSET,
	'A': UNPROT | FSET,
	'9': PROT | ASKIP | BRT | DET | FSET,
	'\'': PROT | ASKIP | DRK | FSET,
	'5': PROT | ASKIP | DET | FSET,
	'Z': PROT | BRT | DET | FSET,
	'1': PROT | ASKIP | FSET,
	'_': PROT | DRK | FSET,
	'V': PROT | DET | FSET,
	'/': PROT | FSET,
	'Q': UNPROT | NUM | BRT | DET,
	'*': UNPROT | NUM | DRK,
	'M': UNPROT | NUM | DET,
	'H': UNPROT | BRT | DET,
	'D': UNPROT | DET,
	'<': UNPROT | DRK,
	'&': UNPROT | NUM,
	' ': UNPROT,
	'8': PROT | ASKIP | BRT | DET,
	'4': PROT | ASKIP | DET,
	'@': PROT | ASKIP | DRK,
	'Y': PROT | BRT | DET,
	'%': PROT | DRK,
	'U': PROT | DET,
	'0': PROT | ASKIP,
	'-': PROT
};

function Field(f, r, c, a, l, d)
{
	this.fieldName = f;
	this.row = parseInt(r);
	this.column = parseInt(c);
	this.orgAttr = a;
	this.attr = getAttrBits(a);
	this.length = parseInt(l);
	this.def = d;
	this.source = "";
	this.style = "";
	this.className = "";
	this.align = "";
	this.custom = false;
	this.format = "";
	this.hide = false;
	this.changed = false;
	this.toString=fieldString;
	return this;
}

function field2Str(f)
{
	return "FIELD("
		+ f.fieldName + ","
		+ f.row + ","
		+ f.column + ","
		+ f.length + ","
		+ (f.orgAttr == "'" ? "&quot;" : f.orgAttr) + "->" +  attr2Str(f.attr) + ","
		+ "\"" + f.def + "\","  
		+ f.source + ","
		+ f.style + ","
		+ f.className + ","
		+ f.align + ","
		+ f.format + ","
		+ f.hide + ","
		+ f.custom + ","
		+ f.changed
		+ ")";
}

function fieldString()
{
	return "Field("
		+ this.fieldName + ","
		+ this.row + ","
		+ this.column + ","
		+ this.length + ","
		+ (this.orgAttr == "'" ? "&quot;" : this.orgAttr) + "->" +  attr2Str(this.attr) + ","
		+ "\"" + this.def + "\","  
		+ this.source + ","
		+ this.style + ","
		+ this.className + ","
		+ this.align + ","
		+ this.format + ","
		+ "hide:" + this.hide + ","
		+ this.custom + ","
		+ this.changed
		+ ")";
}

function field2Key(f)
{
	return f.fieldName != "" ? f.fieldName : f.row + "." + f.column;
}

function getAttrBits(which)
{
	try
	{
		if (which == null)
			return 0;

		var a = ATTR_MAP[which];
		
		return typeof(a) == "undefined" || a == null ? 0 : a;
	}
	catch (e)
	{
		return 0;
	}
}

function attr2Str(attr)
{
	var s = "";
	
	if (checkAttr(attr, PROT))
	{
		if (s != "")
			s += " | ";
		s += "PROT";	
	}
		
	if (checkAttr(attr, NUM))
	{
		if (s != "")
			s += " | ";
		s += "NUM";	
	}
		
	if (checkAttr(attr, BRT))
	{
		if (s != "")
			s += " | ";
		s += "BRT";	
	}
		
	if (checkAttr(attr, DET))
	{
		if (s != "")
			s += " | ";
		s += "DET";	
	}
		
	if (checkAttr(attr, FSET))
	{
		if (s != "")
			s += " | ";
		s += "FSET";	
	}
		
	if (checkAttr(attr, DRK))
	{
		if (s != "")
			s += " | ";
		s += "DRK";	
	}
		
	if (checkAttr(attr, ASKIP))
	{
		if (s != "")
			s += " | ";
		s += "ASKIP";	
	}
	
	if (checkAttr(attr, UNPROT))
	{
		if (s != "")
			s += " | ";
		s += "UNPROT";	
	}
	
	s += " (" + attr.toString(16).toUpperCase() + ")";
		
	return s;
}

function checkAttr(attr, bits)
{
	return (attr & bits) == bits;
}

var ERROR_MAP = 
{
	4201: "INTERNAL ERROR! COULD'NT FIND MAPSET/NAME FROM DATA",
	4202: "INTERNAL ERROR! COULD'NT FIND FIELD INFO FROM MAP",
	4203: "INTERNAL ERROR! COULD'NT FIND DATA FIELDS",
	4204: "INTERNAL ERROR! FAILED TO CREATE ROW MAP",
	4205: "INTERNAL ERROR! FAILED TO CREATE FIELD MAP",
	4206: "INTERNAL ERROR! FAILED TO OBTAIN CUSTOM FIELD MAP"
};

function showError(e, m)
{
	if (e == 0)
	{
		divError.style.display = "none";
		return;
	}
	
	var msg = "ERROR -" + e + ": ";
	
	msg += typeof(ERROR_MAP[e]) == "undefined" ? " -------- UNDEFINED ERROR -------- " : ERROR_MAP[e];
	
	if (m != null)
		msg += " [" + m + "]";
		
	if (divView.checkMode(TOOL))
		divConsole.println(msg);
		
	divError.innerText = msg;
	divError.style.display = "block";
}

function getValue(n, p, d)
{
	try
	{
		return n.selectSingleNode(p).nodeValue;
	}
	catch (e)
	{
		return d;
	}
}

function getText(n, p, d)
{
	try
	{
		return n.selectSingleNode(p).childNodes(0).nodeValue;
	}
	catch (e)
	{
		return d;
	}
}

function getAttr(n, a, d)
{
	try
	{
		return n.attributes.getNamedItem(a).nodeValue	
	}
	catch (e)
	{
		return d;
	}
}



function getCookie(name, def)
{
	var cookies = document.cookie.split("; ");

	for (var i = 0; i < cookies.length; i++)
	{
		var keyValue = cookies[i].split("=");
		if (name == keyValue[0]) 
			return unescape(keyValue[1]);
	}

	return def;
}

function setCookie(name, value)
{
	document.cookie = name + "=" + escape(value) + ";";
}

function Data(n, a, f, v)
{
	this.fieldName = n;
	this.orgAttr = a;
	this.attr = getAttrBits(a);
	this.flags = f;
	this.value = v;
	this.toString = dataString;
	return this;
}

function data2Str(d)
{
	return "DATA("
		+ d.fieldName + ", "
		+ d.orgAttr + "->" + attr2Str(d.attr) + ", "
		+ d.flags + ", "
		+ "\"" + d.value + "\""
		+ ")";	
}

function dataString()
{
	return "DATA("
		+ this.fieldName + ", "
		+ this.orgAttr + "->" + attr2Str(this.attr) + ", "
		+ this.flags + ", "
		+ "\"" + this.value + "\""
		+ ")";	
}

function padMe(v, s)
{
	for (var i = v.length; i < s; i++)
		v += ' ';
	return v;
}

function CustomMap()
{
	this.areaList = new Array();
	this.fieldList = new Array();
	this.funcList = new Array();
    this.selList = new Array();
	this.buttonList = new Array();
	this.toString = CustomMapString;
	return this;
}

function CustomMapString()
{
	return "AreaList : " + this.areaList + " \n FieldList : " + this.fieldList +
		" \n FuncList : " + this.funcList + " \n SelList : " + this.selList + " \n buttonList : " + this.buttonList;
}

function Area(t, l, b, r, c, style)
{
	this.top = t;
	this.left = l;
	this.bottom = b;
	this.right = r;
	this.className = c;
	this.style=style;
	this.skip = 1;
	this.appliesTo = "";
	this.border = ""; //Janak
	this.borderType = ""; //Janak
	this.align = ""; //Janak
	this.column = 0; //Janak
	this.length = 0; //Janak
	this.toString = AreaString;
	return this;	
}

function AreaString()
{
	return "AREA("
		+ this.top + ","
		+ this.left + ","
		+ this.bottom + ","
		+ this.right + ","
		+ this.className + ","
		+ this.style + ","
		+ this.skip + ","
		+ this.appliesTo + ","
		+ this.border + "," //Janak
		+ this.borderType + "," //Janak
		+ this.align + "," //Janak
		+ this.column + "," //Janak
		+ this.length //Janak
		+ ")";	
}

function area2Str(a)
{
	return "AREA("
		+ a.top + ","
		+ a.left + ","
		+ a.bottom + ","
		+ a.right + ","
		+ a.className + ","
		+ a.style + ","
		+ a.skip  + ","
		+ a.appliesTo + ","
		+ a.border + "," //Janak
		+ a.borderType + "," //Janak
		+ a.align + "," //Janak
		+ a.column + "," //Janak
		+ a.length //Janak
		+ ")";	
}

function hasObject(v)
{
	return v != null && typeof(v) != "undefined";
}

function trim(s)
{
	try
	{
		return s.replace(/(^\s+|\s+$)/g, "");
	}
	catch (e)
	{
		return s;
	}
}

function urlEncode(str)
{
	str = escape(str);
	str = str.replace(/%20/g, "+");
	return str;
}

function customFunc(name,appliesTo,fieldList)
{
	this.funcName = name;
	this.appliesTo = appliesTo;
	this.fieldList = fieldList;
	this.toString = customFuncString;
}

function customFuncString()
{
	return "FuncName :" + this.funcName + " \n FieldList :" + this.fieldList +  " \n Applies To : " + this.appliesTo;
}

function loc( x, y)
{
	this.x = x;
	this.y = y;
	this.toString = locString;
}

function locString()
{
	return this.x +  " : " + this.y;
}

function FieldDetail( type, value, showWhen)
{
	this.type = type;
	this.value = value;
	this.display = "";
	this.name = "";
	this.showWhen = showWhen;
	this.toString = FieldDetailString;

}

function FieldDetailString()
{
	return this.type +  " : " + this.value +  " : " + this.display +  " : " + this.name + " : " + this.showWhen;
}

/********************************************************************************
*	Language 		: 	JavaScript 1.2											*
*	Parameter(s) 	: 	Any string 												*
*	Returns 		: 	The same string after trim								*
*	Functionality	: 	The string after removing the leading and trailing 		*
*						whitespace characters (" /n/t/r")						*
********************************************************************************/

function all_trim(str) {
	  	
	startPosition = -1;
  	endPosition = -1;
  	len = str.length;
	
	if (len < 1) {
		return str;
	}
	
  	for (i = (len - 1); i >= 0; i--) {
    	ch = str.charAt(i);
    	if ((ch == ' ') || (ch == '\n') || (ch == '\r') || (ch == '\t')) {
      		continue;
    	} 
		else {
      		endPosition = i ;
			break;
    	}
  	}

  	for (i = 0; i < len; i++) {
    	ch = str.charAt(i);
    	if ((ch == ' ') || (ch == '\n') || (ch == '\r') || (ch == '\t')) {
      		continue;
    	} 
		else {
      		startPosition = i ;
      		break;
    	}
  	}

  	return str.substring(startPosition, endPosition + 1);
} 

/* Janak - printing rowMap */
function printRowMap(rowMap) {

	for(var i=1; i<=rowMap.length; i++)
	{
		var x = rowMap[i];

		if(x != null && x != "undefined")
		{
			//divConsole.print(x);
			//divConsole.print(x.length);

			for(var j=0; j<x.length; j++)
				divConsole.print("[" + i + "][" + j + "]: " + x[j] + " | ");
		}

		divConsole.println("");
	}
}

function trim(str)
{
	if(str.length < 1)
	{
		return "";
	}

	str = rTrim(str);
	str = lTrim(str);

	return str;
}


function rTrim(str)
{
	var w_space = String.fromCharCode(32);
	var v_length = str.length;
	var strTemp = "";

	if(v_length < 0)
	{
		return "";
	}

	var iTemp = v_length -1;

	while(iTemp > -1)
	{
		if(str.charAt(iTemp) == w_space){}

		else
		{
			strTemp = str.substring(0, iTemp+1);
			break;
		}
		iTemp = iTemp-1;
	}

	return strTemp;
}


function lTrim(str)
{
	var w_space = String.fromCharCode(32);

	if(v_length < 1)
	{
		return "";
	}

	var v_length = str.length;
	var strTemp = "";
	var iTemp = 0;

	while(iTemp < v_length)
	{
		if(str.charAt(iTemp) == w_space){}

		else
		{
			strTemp = str.substring(iTemp,v_length);
			break;
		}
		iTemp = iTemp + 1;
	}

	return strTemp;
}